import unittest

import requests
from parameterized import parameterized

from api.rechargeApi import RechargeApi
from api.regLoginApi import RegLoginApi
from common import utils
from common.utils import html_util


class TestRecharge(unittest.TestCase):

    def setUp(self) -> None:
        # 创建session对象
        self.session = requests.session()
        self.reg_login_api = RegLoginApi(self.session)
        self.recharge_api = RechargeApi(self.session)

    # 实例化登录模块封装的类
    # 实例化充值模块封装的类

    def tearDown(self) -> None:
        self.session.close()

    # 关闭session对象

    # 获取充值验证码测试用例
    @parameterized.expand(utils.read_json('test_recharge', "img_verify_code"))
    def test01_get_recharge_verify_code(self, r, exp_status_code):
        # 获取充值图片验证码
        resp = self.recharge_api.get_recharge_verify_code(r)
        # 断言
        self.assertEqual(exp_status_code, resp.status_code)

    # 充值测试用例
    @parameterized.expand(utils.read_json('test_recharge', "recharge"))
    def test02_recharge(self, paymentType, amount, formStr, valicode, exp_status_code, description):
        # 登录
        self.reg_login_api.user_login('15814662599', '123456a')
        # 获取充值图片验证码
        self.recharge_api.get_recharge_verify_code(12345)
        # 充值
        body = {"paymentType": paymentType, "amount": amount, "formStr": formStr, "valicode": valicode}
        resp = self.recharge_api.recharge(body)
        data_json = resp.json()
        print(f'充值: {data_json}')
        # 断言
        # self.assertEqual(200, resp.status_code)
        # self.assertIn('form', data_json.get('description'))
        utils.common_assert(self, resp, exp_status_code, description)

    # 第三方充值测试用例
    def test03_third_recharge(self):
        # 登录
        self.reg_login_api.user_login('15814662599', '123456a')
        # 获取充值图片验证码
        self.recharge_api.get_recharge_verify_code(12345)
        # 充值
        body = {"paymentType": "chinapnrTrust", "amount": "100000", "formStr": "reForm", "valicode": "8888"}
        resp = self.recharge_api.recharge(body)
        # 获取第三方开户接口的url
        url = html_util(resp)[0]
        # 获取第三方开户接口的请求体数据
        body = html_util(resp)[1]
        # 获取第三方接口返回的响应结果
        resp = self.recharge_api.third_recharge(url, body)
        data_text = resp.text
        print(f'第三方充值: {data_text}')
        # 断言
        self.assertEqual(200, resp.status_code)
        self.assertIn('OK', data_text)
